// PLAYER
var Player = function(){
	
	this.game = null;
	this.speed = 5;
	this.spritesImg = [{
		img: 	'',
		x: 		10,
		y: 		150,
		w: 		30,
		h: 		30
	}];	
	this.bounds = {
		x: 	MAX_WIDTH/2-15,
		y: 	MAX_HEIGHT/2-15,
		w: 	10,
		h: 	10,
		radius: 10,
		minRadius: 0
	};
	this.bounds.minRadius = this.bounds.radius/2;
 	this.spritesRes = [];
 	this.alive = true;
	this.color = 'rgba('+COLORS['r'][0]+', '
					    +COLORS['g'][0]+', '
					    +COLORS['b'][0]+', 1)';
	this.type = 0;

	this.context = null;

	this.shadow = 15;
	this.shadowGrow = 1;

	this.followers = 0;
	this.avoiders = 0;
	this.nextStep = 0;
	this.toKill = 0;

	this.target = {
		x: 	MAX_WIDTH/2-15,
		y: 	MAX_HEIGHT/2-15
	}

	this.init = function(parentGame) {
		this.speed *= GAME_SPEED;
		this.game = parentGame;
		this.loadSprites();
		this.changeColor(0);

		MOUSE.x = this.target.x;
		MOUSE.y = this.target.y;
	};

	this.update = function() {

		// log("x:"+this.bounds.x+" y:"+this.bounds.y+" w:"+this.bounds.w+" h:"+this.bounds.h);

		this.followers = ((this.type - 1) <= 0) ? 3 : this.type - 1;
		this.avoiders  = ((this.type + 1) >  3) ? 1 : this.type + 1;

		if(this.type == 0) this.followers = this.avoiders = 0;

		this.input();
		this.move();
	}

	this.changeColor = function(type) {
		this.type = type;
		game.warning = type;
		game.timeBeforeWarning = game.TIME;
		//Défini la couleur
		this.color = 'rgba('+COLORS['r'][type]+', '
							+COLORS['g'][type]+', '
							+COLORS['b'][type]+', 1)';
	}

	// Chargement des sprites du character
 	this.loadSprites = function() {
 		var is = 0;
 		while( is < this.spritesImg.length ) {
 			var img = new Image();
 			img.src = this.spritesImg[is].img;
 			this.spritesRes.push( img );
 			is++;
 		}
 	};

	this.input = function(){
		// if( getKey('1').state ) {
		// 	this.color = "rgba(254,0,45,1)";
		// }
		// if( getKey('2').state ) {
		// 	this.color = "rgba(45,254,0,1)";
		// }
		// if( getKey('3').state ) {
		// 	this.color = "rgba(0,97,254,1)";
		// }		
		// if( getKey('up').state ) {
		// 	this.resize( 1 );
		// }
		// if( getKey('down').state ) {
		// 	this.resize( -1 );
		// }
	}

	this.render = function() {
		if(this.hit){
			this.flash('red');
		}
		
		// Shadow
		this.context.fillStyle = this.color;
		this.context.beginPath();
		this.context.arc(this.bounds.x,this.bounds.y,this.bounds.radius,0,Math.PI*2,true);
		this.context.shadowColor = this.color;
		this.context.shadowBlur = this.shadow;
		this.context.shadowOffsetX = 0;
		this.context.shadowOffsetY = 0;
		this.context.fill();
		
		// Pulse shadow
		if( this.shadow >= 40 || this.shadow <= 10)
			this.shadowGrow *= -1;
		this.shadow += 0.4 * this.shadowGrow;
		
		this.renderChainColor(this.nextStep);

		// drawRect(ctx, "#FFF", this.target.x, this.target.y, 5, 5);
	}

	this.renderChainColor = function(n){
		this.context.shadowBlur = 0;
		//Constante de l'angle
		var N = ((2 * Math.PI) / n)
		var nIni = n;
		var offset = 1 + this.bounds.radius;
		this.context.beginPath();
		while(n --){
			//Variable selon le n
			var angle = N * n;
			this.context.beginPath();
			this.context.arc(this.bounds.x + Math.sin(angle) * offset,
						this.bounds.y + Math.cos(angle) * offset,
						2,
						0,
						Math.PI*2,true);

			if (n >= this.toKill) {
				this.context.fillStyle = 'rgba('+COLORS['r'][this.avoiders]+', '
												+COLORS['g'][this.avoiders]+', '
												+COLORS['b'][this.avoiders]+', 1)';
			}
			else {
				this.context.fillStyle = 'rgba('+COLORS['r'][0]+', '
												+COLORS['g'][0]+', '
												+COLORS['b'][0]+', .1)';
				
			}
			this.context.closePath();
			this.context.fill();
		}
		
	}

	this.flash = function(color){
		switch (color) {
			case 'red': 	this.context.fillStyle = "rgba(168, 3, 18, .7)";
							break;

			case 'white': 	this.context.fillStyle = "rgba(255, 255, 255, .7)";
							break;

			case 'blue': 	this.context.fillStyle = "rgba(0, 255, 255, .4)";
							break;

			case 'orange':  this.context.fillStyle = "rgba(229, 73, 16, .8)";
							break;

			case 'purple':  this.context.fillStyle = "rgba(191, 11, 113, .8)";
							break;
		}
		
		this.context.fillRect(0, 0, MAX_WIDTH, MAX_HEIGHT);
		this.hit = false;
	}

	this.move = function() {

		if(typeof window.deviceorientation == "undefined") {
			this.target.x = MOUSE.x;
			this.target.y = MOUSE.y;
		}

		if(Math.round(distance(this.bounds, this.target)) < 10) {
			this.bounds.x = this.target.x;
			this.bounds.y = this.target.y;
		}

		var angle = Math.atan2(this.target.y - this.bounds.y, this.target.x - this.bounds.x);
		newX = this.bounds.x + Math.cos(angle) * this.speed;
		newY = this.bounds.y + Math.sin(angle) * this.speed;

		this.checkProps(newX,newY);
		if (this.checkBoundary(newX,newY)) {
			this.bounds.x = newX;
			this.bounds.y = newY;
		} else {
			if (newX - this.bounds.radius < 0) newX = 0 + this.bounds.radius;
			else if (newX + this.bounds.radius > MAX_WIDTH) newX = MAX_WIDTH - this.bounds.radius;

			if (newY - this.bounds.radius < 0) newY = 0 + this.bounds.radius;
			else if (newY + this.bounds.radius > MAX_HEIGHT) newY = MAX_HEIGHT - this.bounds.radius;

			this.bounds.x = newX;
			this.bounds.y = newY;
		}
	}
	
	// Changement de taille de la bestiole
	this.resize = function(ratio) {

		if( this.bounds.radius + ratio <= this.bounds.minRadius ) {
			this.alive = false;
			game.warning = 4;
		}
		else if( this.bounds.radius > MAX_WIDTH / 3 / 2 ) {
			this.bounds.radius = MAX_WIDTH / 3 / 2;
			this.bounds.x = this.bounds.radius * 2;
			this.bounds.y = this.bounds.radius * 2;
		}
		else {
			this.bounds.radius += ratio;
			this.bounds.x = this.bounds.radius * 2;
			this.bounds.y = this.bounds.radius * 2;
			this.nextStep = this.bounds.radius;
		}
	}
	
	// GESTION DE LA COLLISION
	// Retourne False si hors du terrain
	this.checkBoundary = function(axeX,axeY) {
		if (axeX - this.bounds.radius < 0 || 
			axeY-this.bounds.radius < 0 || 
			axeX+this.bounds.radius > MAX_WIDTH || 
			axeY+this.bounds.radius > MAX_HEIGHT) return false;
		else return true;	
	}

	// CHECK PROPS
	// Retourn False si collision
	this.checkProps = function(axeX,axeY) {
		var result = true;
		var r = this.bounds.radius*3;
		var malus = -2;
		var nocolor = false;
		for (var iArray = 1; iArray < this.game.enemies.length; iArray++) {
			for (var iEn = 0; iEn < this.game.enemies[iArray].length; iEn++) {
				if (!this.game.enemies[iArray][iEn].collide(axeX,axeY)) {					
					if (game.bonus != null) {
						if (game.bonus.type == 'nocolor' && game.bonusState) {
							this.game.enemies[iArray][iEn].kill();
							this.game.enemies[iArray][iEn].repop(2);
							nocolor = true;
						}
					}
					if (!nocolor){
						if (this.type != this.game.enemies[iArray][iEn].type) {
							if (this.type == 0) {
								this.changeColor(this.game.enemies[iArray][iEn].type);			
								this.nextStep = this.bounds.radius;
								game.SCORE += 500;
								this.toKill = 10;
							}
							else {
								if(this.checkConsumable(this.game.enemies[iArray][iEn].type,malus)) {
									this.game.enemies[iArray][iEn].kill();
								}
								if (this.toKill <= 0) {
									this.changeColor(this.game.enemies[iArray][iEn].type);
									this.resize(3);
									game.SCORE += 500;
									this.toKill = 10;
								}
							}
							this.game.enemies[iArray][iEn].repop(2);						
						}
					}
					result = false;
				}
			}
		}
		
		// Test de collision sur les bonus
		if (game.bonus != null) {
			if (game.bonus.collide(axeX, axeY)) {
				game.bonus.activate();
				game.bonusState = true;
				game.bonus.disappear = true;
				if(this.game.bonus.type == 'gravity')
					this.game.bonus.showGravity(20);
			}
		}
		
		return result;
	}

	// CHECK SI LE PROPS EST CONSOMMABLE
	this.checkConsumable = function(type,malus) {
		var consumable = false;
		switch (type) {
			case 0 : break;

			case 1 : 	if (this.type == 3 || this.type == 0) {
							consumable = true;
						}
						else if (this.type == 2){ this.resize(malus); this.hit = true;};
						break;

			case 2 : 	if (this.type == 1 || this.type == 0) {
							consumable = true; 
						}
						else if (this.type == 3){ this.resize(malus); this.hit = true;};
						break;

			case 3 : 	if (this.type == 2 || this.type == 0) {
							consumable = true; 
						}
						else if (this.type == 1){ this.resize(malus); this.hit = true;};
						break;

			default : break;
		}
		return consumable;
	}
};